package com.mvp.shq.washer.view.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.mvp.shq.washer.R;
import com.mvp.shq.washer.adapter.RefundReasonAdapter;
import com.mvp.shq.washer.adapter.TypeChoseAdapter;
import com.mvp.shq.washer.entity.shop.RefundReason;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by shq1 on 2017/10/12.
 * 退货原因选择弹出框
 */

public class RefundTypePopupWindow extends PopupWindow {

    private View mPopView;
    private Activity activity;
    private ViewHolder holder;
    private String mTitleName;
    private int currerPosition;
    private OnCheckListener onCheckListener;
    private int selectPosition = 0;
    private List<RefundReason> data;
    private RefundReasonAdapter adapter;
    public int getCurrerPosition() {
        return currerPosition;
    }

    public void setCurrerPosition(int currerPosition) {
        this.currerPosition = currerPosition;
    }

    public RefundTypePopupWindow(Context context, Activity mActivity, OnCheckListener onCheckListener) {
        super(context);
        this.activity = mActivity;
        this.onCheckListener=onCheckListener;
        // TODO Auto-generated constructor stub
        init(context);
        setPopupWindow();
    }

    /**
     * 初始化
     *
     * @param context
     */
    @SuppressLint("WrongViewCast")
    private void init(Context context) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = LayoutInflater.from(context);
        //绑定布局
        mPopView = inflater.inflate(R.layout.pop_refundtype, null);
        holder = new ViewHolder(mPopView);
        String[] GENRES = {"不喜欢/效果差","质量问题","物品与商品描述不符","卖家发错货","收到商品少件/破损/污渍等","假冒品牌","其他"};
        holder.bntClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onCheckListener.onCheck(data.get(selectPosition).getName());
                dismiss();
            }
        });
        data = new ArrayList<>();
        for (int i=0;i<GENRES.length;i++){
            RefundReason reason = new RefundReason(GENRES[i],false);
            data.add(reason);
        }
        data.get(selectPosition).setSelect(true);
        adapter = new RefundReasonAdapter(context,data);
        holder.listView.setAdapter(adapter);
        holder.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == selectPosition){
                    return;
                }
                data.get(selectPosition).setSelect(false);
                data.get(position).setSelect(true);
                selectPosition = position;
                adapter.notifyDataSetChanged();
            }
        });
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                lp.alpha = 1f;
                activity.getWindow().setAttributes(lp);
            }
        });

    }

    /**
     * 设置窗口的相关属性
     */
    @SuppressLint("InlinedApi")
    private void setPopupWindow() {
        this.setContentView(mPopView);// 设置View
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);// 设置弹出窗口的宽
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);// 设置弹出窗口的高
        this.setFocusable(true);// 设置弹出窗口可
        this.setAnimationStyle(R.style.popwindow_anim_bottom);// 设置动画
        this.setBackgroundDrawable(new BitmapDrawable());// 设置背景透明
        //this.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mPopView.setOnTouchListener(new View.OnTouchListener() {// 如果触摸位置在窗口外面则销毁
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                int height = mPopView.findViewById(R.id.id_pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });


    }

    public class ViewHolder {
        @BindView(R.id.tv_titlename)
        TextView tvTitlename;
        @BindView(R.id.bnt_close)
        TextView bntClose;
        @BindView(R.id.lv_refundtype)
        CustomListView listView;
        public ViewHolder(View itemView) {
            ButterKnife.bind(this, itemView);
        }
    }
    public interface OnCheckListener{
        void onCheck(String content);
    }
}
